package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.eventbus.WechatBackMessageEvent;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.UserInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.utils.Utils;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class PersonalInfoActivity extends BaseActivity {
    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.txt_real_name)
    TextView txtRealName;
    @InjectView(R.id.txt_id_num)
    TextView txtIdNum;
    @InjectView(R.id.txt_phoneNum)
    TextView txtPhoneNum;
    @InjectView(R.id.not_realname_tip_layout)
    LinearLayout notRealnameTipLayout;
    @InjectView(R.id.realname_operation_layout)
    LinearLayout realnameOperationLayout;
    @InjectView(R.id.unbind_layout)
    LinearLayout unbindLayout;
    @InjectView(R.id.txt_nickname)
    TextView txtNickname;
    private DBOpenHelper helper;
    private String wechatCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);
        EventBus.getDefault().register(this);
        ButterKnife.inject(this);
        helper = new DBOpenHelper(PersonalInfoActivity.this);
        initView();
        checkIsBindWechat();
    }


    private void initView() {
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText("个人信息");
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        txtPhoneNum.setText(Utils.hideMidFourPhoneNum(helper.getMobile()));
        if (helper.getIdCard().equals("")) {
            txtIdNum.setText("未认证");
            notRealnameTipLayout.setVisibility(View.VISIBLE);
            realnameOperationLayout.setVisibility(View.VISIBLE);
        } else {
            txtIdNum.setText(Utils.hideIdNum(helper.getIdCard()));
        }
        if (helper.getRealName().equals("")) {
            txtRealName.setText("未认证");
        } else {
            txtRealName.setText(Utils.hideRealName(helper.getRealName()));
        }
    }

    private void checkIsBindWechat() {
        ActionUtil actionUtil = new ActionUtil(PersonalInfoActivity.this);
        actionUtil.checkIsBindWechat();
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                UserInfo info = (UserInfo) model;
                if (info == null) {
                    UIHelper.showToast(PersonalInfoActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (info.getErrorMsg() != null) {
                    if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(PersonalInfoActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(PersonalInfoActivity.this, info.getErrorMsg());
                    }
                } else {
                    if ("true".equals(info.getStatus())) {
                        txtNickname.setVisibility(View.VISIBLE);
                        txtNickname.setText(info.getNickname());
                    } else {
                        unbindLayout.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    private void skipToMain() {
        Intent intent = new Intent();
        intent.setClass(PersonalInfoActivity.this, MainActivity.class);
        intent.putExtra("type", "invest");
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onWechatBackEvent(WechatBackMessageEvent event) {
        wechatCode = event.getCode();
        bindWeChat(wechatCode);
    }

    private void bindWeChat(String wechatCode) {
        ActionUtil actionUtil = new ActionUtil(PersonalInfoActivity.this);
        actionUtil.bindWechat(wechatCode);
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                UserInfo info = (UserInfo) model;
                if (info == null) {
                    UIHelper.showToast(PersonalInfoActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (info.getErrorMsg() != null) {
                    if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(PersonalInfoActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(PersonalInfoActivity.this, info.getErrorMsg());
                    }
                } else {
                    if ("true".equals(info.getStatus())) {
                        txtNickname.setVisibility(View.VISIBLE);
                        unbindLayout.setVisibility(View.GONE);
                        txtNickname.setText(info.getNickname());
                    }
                }
            }
        });
    }


    @OnClick({R.id.btn_invest, R.id.btn_recharge, R.id.unbind_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_invest:
                skipToMain();
                break;
            case R.id.btn_recharge:
                ActivityUtils.gotoRecharge(PersonalInfoActivity.this);
                break;
            case R.id.unbind_layout:
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "goldplusgold";
                MyApplication.wxapi.sendReq(req);
                break;
        }
    }

}
